<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

<!--      <el-form-item label="领养记录" prop="adoptionId">-->
<!--        <el-input-->
<!--          v-model="queryParams.adoptionId"-->
<!--          placeholder="输入领养记录名"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->


<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-s-home"
          size="mini"
          @click="handleQuery(1)"
        >我的申请</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-s-custom"
          size="mini"
          @click="handleQuery(2)"
        >别人申请</el-button>
      </el-col>
      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyAdoptionList" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="标题">
              <span style="font-weight: bolder">{{ props.row.title }}</span>
            </el-form-item>
            <el-form-item label="发布者">
              <span>{{ props.row.promNickName }}</span>
            </el-form-item>
            <el-form-item label="描述">
              <span v-html="props.row.content"></span>
            </el-form-item>
            <el-form-item label="图片">

              <div style="display: flex;">
                <div v-for="(item) in props.row.imgUrlList" style="margin: 10px">
                  <img :src="item"  @click="picture(item)"   :width="100" :height="100">
                </div>
              </div>
            </el-form-item>
            <!--            <el-form-item label="是否被领养">-->
            <!--              <span>{{ props.row.petType }}</span>-->
            <!--            </el-form-item>-->
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="申请用户" align="center" prop="applyNickName" />
<!--      <el-table-column label="领养记录" align="center" prop="adoptionId" />-->
      <el-table-column label="是否通过领养" align="center" prop="consentStatus" />
      <el-table-column label="申请理由" align="center" prop="applyReason" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div style="color: #13ce66" v-if="scope.row.isConsent==1">
            已同意
          </div>
          <el-button
            v-if="scope.row.isConsent==0 && scope.row.promUserId==userId"
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handleUpdate(scope.row,1)"
            v-hasPermi="['petSys:applyAdoption:edit']"
          >同意</el-button>
          <el-button
            v-if="scope.row.promUserId==userId"
            size="mini"
            type="text"
            icon="el-icon-s-comment"
            @click="chatAbout(scope.row)"
            v-hasPermi="['petSys:applyAdoption:edit']"
          >聊天</el-button>
          <div  style="color:red" v-if="scope.row.isConsent==2">
            已拒绝
          </div>
          <el-button
            v-if="scope.row.isConsent==0 && scope.row.promUserId==userId"
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="handleUpdate(scope.row,2)"
            v-hasPermi="['petSys:applyAdoption:remove']"
          >拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改申请领养对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申请领养用户id" prop="applyUserId">
          <el-input v-model="form.applyUserId" placeholder="请输入申请领养用户id" />
        </el-form-item>
        <el-form-item label="发布的领养记录id" prop="adoptionId">
          <el-input v-model="form.adoptionId" placeholder="请输入发布的领养记录id" />
        </el-form-item>
        <el-form-item label="是否通过领养" prop="isConsent">
          <el-input v-model="form.isConsent" placeholder="请输入是否通过领养" />
        </el-form-item>
        <el-form-item label="申请理由" prop="applyReason">
          <el-input v-model="form.applyReason" placeholder="请输入申请理由" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <div class="image-preview">
      <el-image style="width: 0; height: 0" ref="previewImg" :src="url" :preview-src-list="srcList">
      </el-image>
    </div>
  </div>
</template>

<script>
import { listApplyAdoption, getApplyAdoption, delApplyAdoption, addApplyAdoption, updateApplyAdoption } from "@/api/petSys/applyAdoption";
import { addFriendById} from "@/api/petSys/attention";
export default {
  name: "ApplyAdoption",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 申请领养表格数据
      applyAdoptionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applyUserId: null,
        adoptionId: null,
        isConsent: null,
        applyReason: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      userId:"",
      url:"",
      srcList:[]
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.userId = this.$store.state.user.id;
  },
  methods: {
    /** 查询申请领养列表 */
    getList() {
      this.loading = true;
      listApplyAdoption(this.queryParams).then(response => {
        this.applyAdoptionList = response.rows;
        console.log("这是获取的列表",response.rows)
        this.total = response.total;
        this.loading = false;
        this.applyAdoptionList.forEach(item=>{
          if(item.imgUrl != null){
            item.imgUrlList = item.imgUrl.split(";");
            item.imgUrlList.splice(item.imgUrlList.length-1,1)
            for(var i = 0 ;i<item.imgUrlList.length;i++){
              item.imgUrlList[i]= item.imgUrlList[i].replace(/\\/g,"/")
              item.imgUrlList[i]= "/"+item.imgUrlList[i]


            }

          }
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        applyUserId: null,
        adoptionId: null,
        isConsent: null,
        createTime: null,
        updateTime: null,
        applyReason: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery(e) {
      this.queryParams.pageNum = 1;


      if(e==1){
        this.queryParams.promUserId = null;
        this.queryParams.applyUserId = this.userId;
      }else if(e==2){
        this.queryParams.applyUserId = null;
        this.queryParams.promUserId = this.userId;
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.applyUserId = null;
      this.queryParams.promUserId = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加申请领养";
    },
    /** 修改按钮操作 */
    handleUpdate(row,e) {
      this.reset();
      if(e==1){
        this.form.isConsent = 1
      }else if(e==2){
        this.form.isConsent = 2
      }
      this.form.id = row.id
      this.form.adoptionId = row.adoptionId

      updateApplyAdoption(this.form).then(response => {
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApplyAdoption(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApplyAdoption(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除申请领养编号为"' + ids + '"的数据项？').then(function() {
        return delApplyAdoption(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    chatAbout(row){
      console.log("聊天啦",row)

      let data ={
        applyUserId:this.userId,
        beApplyUserId:row.applyUserId
      }
      console.log(data)
      addFriendById(data).then(response => {
        this.$router.push({
          path: '/chat/toChat',
          query: {
            id: row.applyUserId

          }
        })
      })


    },
    picture(value) {
      console.log(value)
      this.srcList = [value]
      this.viewBigPicture()
    },
    viewBigPicture() {
      this.$refs.previewImg.showViewer = true
    }

  }
};
</script>
<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 40%;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.el-upload-list__item-thumbnail {
  width: 100%;
  height: 100%; /* 或者指定一个固定高度 */
  object-fit: cover; /* 这将使图片缩放以适应内容区域，同时保持其纵横比 */
}
div:has(.el-upload-list__item-thumbnail) {
  /* 在这里放置您想要应用到父级div的样式 */
  height: 100%; /* 或者指定一个固定高度 */
}

.dialog-footer {
  position:absolute;
  right: 40px;
  bottom: 30px;
}
</style>
